<?php
use think\Db;
use OSS\OssClient;
use OSS\Core\OssUtil;
use OSS\Http\RequestCore;
use OSS\Result\Result;
use OSS\Result\ExistResult;
require_once 'function.php';
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件

//图片上传至阿里云OSS
function aliyun($savePath='',$category='',$isunlink=false,$bucket="zwadmin"){
    include_once ROOT_PATH.'extend/aliyun-oss-php-sdk-master/src/OSS/OssClient.php';
    include_once ROOT_PATH.'extend/aliyun-oss-php-sdk-master/src/OSS/Core/OssUtil.php';
    include_once ROOT_PATH.'extend/aliyun-oss-php-sdk-master/src/OSS/Http/RequestCore.php';
    include_once ROOT_PATH.'extend/aliyun-oss-php-sdk-master/src/OSS/Http/ResponseCore.php';
    $accessKeyId = config('aliyun_oss.accessKeyId');//去阿里云后台获取秘钥
    $accessKeySecret = config('aliyun_oss.accessKeySecret');//去阿里云后台获取秘钥
    $endpoint = config('aliyun_oss.endpoint');//你的阿里云OSS地址
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);    
    // 判断bucketname是否存在，不存在就去创建
    if( !$ossClient->doesBucketExist($bucket)){
        $ossClient->createBucket($bucket);
    }
    
    $category=empty($category)?$bucket:$category;

    $savePath = str_replace("\\","/",$savePath);

    $object = $category.'/'.$savePath;//想要保存文件的名称
    $file = './uploads/'.$savePath;//文件路径，必须是本地的。
    //  echo $file;
    // exit;
    try{
        $ossClient->uploadFile($bucket,$object,$file);
        if ($isunlink==true){
            unlink($file);
        }
    }catch (OssException $e){
        $e->getErrorMessage();
    }
    $oss=config('aliyun_oss.url');
    return $oss."/".$object;
}